package com.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.R;
import com.reggie.pojo.AddressBook;
import com.reggie.pojo.User;
import com.reggie.service.IUserService;
import com.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);

            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

//            session.setAttribute(phone,code);
            //将原先存在session中的验证码存到redis中
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("手机验证码短信发送成功");
        }
        return R.error("手机验证码短信发送失败");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){//为什么用Map(再封装一个UserDto?)
        log.info(map.toString());
        //获取传过来的手机号和验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //获取session中存的code
//        Object trueCode = session.getAttribute(phone);
//        获取redis中存的code
        Object trueCode = redisTemplate.opsForValue().get(phone);
        //验证码比对，正确则登录成功
        if (trueCode != null && trueCode.equals(code)){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,phone);

            //根据手机号去数据库中找到此条数据
            User user = userService.getOne(lqw);
            if (user == null){
                //新用户就注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);

                userService.save(user);
            }
            //把验证通过的user的id放进session中,供移动端操作时获取使用
            session.setAttribute("user",user.getId());

            //登录成功，删除redis中缓存的验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        return R.error("验证码不正确！");
    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出成功");
    }


}
